package com.zkadmin.renderer;

import java.util.Set;

import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.TreeitemRenderer;
import org.zkoss.zul.Treerow;

import com.zkadmin.entity.SysResource;

public class RoleResourceTreeitemRenderer extends BaseRenderer implements TreeitemRenderer<DefaultTreeNode<SysResource>> {
	private Set<SysResource> resources;

	public RoleResourceTreeitemRenderer(Set<SysResource> resources) {
		this.resources = resources;
	}

	@Override
	public void render(final Treeitem treeitem, DefaultTreeNode<SysResource> data, int index) throws Exception {
		SysResource resource = data.getData();
		if (resources.contains(resource)) {
			treeitem.setSelected(true);
		}

		Treerow treerow = new Treerow();
		treeitem.setAttribute("resourceId", resource.getId());
		treeitem.setOpen(true);
		treeitem.appendChild(treerow);

		createTreecell(treerow).setLabel(resource.getName());
		createTreecell(treerow).setLabel(resource.getPermission());
	}
}
